<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="s" uri="/struts-tags"%>
<%@ page
	import="com.onlineMIS.common.Common_util,java.util.Date,java.text.SimpleDateFormat"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>禧乐仓连锁店管理信息系统</title>
<%@ include file="../../common/Style.jsp"%>

<script>

$(document).ready(function(){
	parent.$.messager.progress('close'); 
	
	$('#headqCustId').combobox('setValue', '');

	var params = $.serializeObject($('#preGenReportForm'));
	
	$('#dataGrid').datagrid({
		url : 'headQReportJSON!getUnitSalesReportInfo',
		queryParams: params,
		fit : true,
		fitColumns : false,
		pagination : false,
		pageSize : 20,
		pageList : [ 20, 30],
		border : false,
		checkOnSelect : false,
		selectOnCheck : false,
		singleSelect:true,
		showFooter:true,
		rownumbers:true,
		nowrap : false,
		columns : [ [
			{field:'clientId', width:70,title:'客户账号',fixed:true},
			{field:'clientName', width:170,title:'客户姓名',fixed:true,
				formatter: function (value, row, index){
					return row.clientName;
				}},
			{field:'totalQuantity', width:80,title:'销售数量 A'},
			{field:'totalRecCost', width:100,title:'销售公司成本',
				formatter: function (value, row, index){
					return row.totalRecCost.toFixed(2);
				}},
			{field:'totalWholePrice', width:120,title:'销售金额 B',
				formatter: function (value, row, index){
					return row.totalWholePrice.toFixed(2);
				}},	
			{field:'totalQuantityR', width:80,title:'退货数量 C'},
			{field:'totalRecCostR', width:100,title:'退货公司成本',
				formatter: function (value, row, index){
					return row.totalRecCostR.toFixed(2);
				}},
			{field:'totalWholePriceR', width:120,title:'退货金额 D',
					formatter: function (value, row, index){
						return row.totalWholePriceR.toFixed(2);
					}},	
			{field:'returnRate', width:70,title:'退货率',
					formatter: function (value, row, index){
						return row.returnRate.toFixed(1) + "%";
					},styler: function(value, row, index) {		
						if(row.returnRate >=14){
							 return 'color: red; background-color: #ffe6e6;'; 	
						}					    				        
			 } },	
			{field:'sumQuantity', width:100,title:'合计数量 A-C'},
			{field:'sumWholePrice', width:120,title:'合计销售金额 B-D',
				formatter: function (value, row, index){
					return row.sumWholePrice.toFixed(2);
				}},
			{field:'sumDmQuantity', width:80,title:'代卖数量'},
			{field:'sumDmNetSalesAmount', width:100,title:'代卖金额',
					formatter: function (value, row, index){
						return row.dmNetSalesAmount.toFixed(2);
					}},
			{field:'sumMdQuantity', width:80,title:'买断数量'},
			{field:'sumMdNetSalesAmount', width:100,title:'买断金额',
						formatter: function (value, row, index){
							return row.mdNetSalesAmount.toFixed(2);
						}},
			{					
				field : 'action',
				title : '详情',
				width : 70,
				formatter : function(value, row, index) {
					var str = '';
					var url = "<%=request.getContextPath()%>/action/headqReportJSP!unitSalesReportDetails?formBean.orderIds=" + row.orderIds;
					if(row.clientId !=0){
						str += $.formatString('<a href="#" onclick="addTab6(\'{0}\',\'{1}\');"><img border="0" src="{2}" title="详情"/></a>', url, '客户 ' + row.clientId,'<%=request.getContextPath()%>/conf_files/easyUI/themes/icons/update.png');									
					}
					return str;
				}
			}													
		]],
		toolbar : '#toolbar',
	});
});


	//查询报表
	function generateReport() {
		var isSuccess = false;
		var yearInfo = $("#yearId").combobox('getValue');
		var quarter = $("#quarterId").combobox('getValue');
		var brand = $("#brandId").combobox('getValue');
		
		// 校验规则：1.有品牌，必须有年份，季度，2.有季度，必须有年份
		
		if(brand !=null && brand != ""){
			if((yearInfo == null || yearInfo == "")
					&& (quarter == null || quarter == "")){
						$.messager.alert('提示', '请选择货品年份季度信息', 'warning');
			}else if ((yearInfo != null || yearInfo != "")
					&& (quarter == null || quarter == "")) {
				$.messager.alert('提示', '请选择货品季度信息', 'warning');
				return;
			} else if ((yearInfo == null || yearInfo == "")
					&& (quarter != null || quarter != "")) {				
				$.messager.alert('提示', '请选择货品年份信息', 'warning');
				return;
			} 
		}
		
		// 当有年份信息的时候，季度不能为空
		if(yearInfo != null && yearInfo != ""){
			if(quarter == null || quarter == ""){
				$.messager.alert('提示', '请选择货品季度信息', 'warning');
				return;
			}else{
				isSuccess = true;
			}
		}
		
		// 当有季度信息的时候，年份不能为空
		if(quarter != null && quarter != ""){
			if(yearInfo == null || yearInfo == ""){
				$.messager.alert('提示', '请选择年份季度信息', 'warning');
				return;
			}else{
				isSuccess = true;
			}
		}else{
			isSuccess = true;
		}
		
		if (isSuccess) {
			var params = $.serializeObject($('#preGenReportForm'));
			$('#dataGrid').datagrid('load', params);
		}
	}

	//下载单据
	function downloadReport() {
		document.preGenReportForm.action = "headqReportJSP!downloadReportInfo";
		document.preGenReportForm.submit();
	}

	function BSkeyDown(e) {

		var ieKey = event.keyCode;

		if (ieKey == 13) {
			if (event.srcElement.id == "custName") {
				searchCust();
				event.returnValue = false;
			}
		}
	}

	//限制年份下拉框
	function onChangeYear() {
		var yearInfo = $("#yearId").combobox('getValue');
		if (yearInfo == null || yearInfo == "") {
			$.messager.alert('提示', '请先选择货品年份信息', 'warning');
		}
	}	
	
	function selectBarcode(){
		var barcode = $("#barcode").textbox('getValue');
		if(barcode != null && barcode !=""){
			$("#yearId").textbox('disable');
			$("#quarterId").textbox('disable');
			$("#brandId").textbox('disable');
		}else{
			$("#yearId").textbox('enable');
			$("#quarterId").textbox('enable');
			$("#brandId").textbox('enable');
		}
	}
	


	document.onkeydown = BSkeyDown;
</script>
</head>
<body>
	<div class="easyui-layout" data-options="fit : true,border : false">
		<div data-options="region:'north',border:false" style="height: 130px;">
			<s:form id="preGenReportForm" name="preGenReportForm" action="" theme="simple" method="POST">
				<table width="98%" align="center" class="OuterTable">
					<tr>
						<td>
							<table width="100%" border="0">
								<tr>
									<td height="50" colspan="13">
										<div class="errorAndmes">
											<s:actionerror cssStyle="color:red" />
											<s:actionmessage cssStyle="color:blue" />
										</div>
										<table width="100%" border="0">
											<tr class="PBAOuterTableTitale">
												<td height="35" colspan="12">客户账目流水信息：</td>
											</tr>
											<tr class="InnerTableContent">

												<td width="3%" align="center"><strong>日期:</strong></td>
												<td width="20%"><s:textfield id="startDate"
														name="formBean.startDate" cssClass="easyui-datebox"
														data-options="width:120,editable:false" /> &nbsp; 至&nbsp;
													<s:textfield id="endDate" name="formBean.endDate"
														cssClass="easyui-datebox"
														data-options="width:120,editable:false" /></td>
												<td width="4%" align="center"><strong>货品条码:</strong></td>
												<td width="12%">
													<s:textfield id="barcode" style="width:83%"
														cssClass="easyui-textbox"
														data-options="prompt:'请输入货品条码',onChange:function(a,b){selectBarcode();}"
														name="formBean.barcode" size="12" />
												</td>
												<td align="center"><strong>年份:</strong></td>
												<td><s:select size="1" cssClass="easyui-combobox"
														style="width:75%" data-options="prompt:'请选择年份'"
														id="yearId" list="uiBean.years" listKey="year_ID"
														name="formBean.year.year_ID" listValue="year" headerKey=""
														headerValue="" /></td>
												<td align="center"><strong>季度:</strong></td>
												<td><s:select size="1" cssClass="easyui-combobox"
														style="width:80%"
														data-options="prompt:'请选择季度',editable:false"
														id="quarterId" list="uiBean.quarters" listKey="quarter_ID"
														name="formBean.quarter.quarter_ID"
														listValue="quarter_Name" headerKey="" headerValue="" /></td>
												<td align="center"><strong>品牌:</strong></td>
												<td><s:select size="1" cssClass="easyui-combobox"
														style="width:80%" data-options="prompt:'请选择品牌'"
														id="brandId" list="uiBean.brands" listKey="brand_ID"
														name="formBean.brand.brand_ID" listValue="brand_Name"
														headerKey="" headerValue="" /></td>
												<td align="center"><strong>货品归属:</strong></td>
												<td><s:select size="1" cssClass="easyui-combobox"
														style="width:85%" data-options="prompt:'请选择归属'"
														id="belongTo" list="uiBean.belongToVOs" listKey="id"
														listValue="belongToName" name="formBean.brand.belong_To" /></td>
												<td></td>
												
											</tr>
											<tr>

												<td align="center"><strong>客户:</strong></td>
												<td>
														<s:select size="1"
														cssClass="easyui-combobox" style="width:65%"
														data-options="prompt:'请选择客户',onChange:function(a,b){selectCust();}"
														headerKey="" headerValue="" id="headqCustId"
														list="uiBean.custs" listKey="id" name="formBean.cust.id"
														listValue="name" />
														</td>
												<td width="5%" align="center"><strong>客户标签：</strong></td>
												<td colspan="9">
														<input class="easyui-tagbox" name="formBean.tagGroups" id="tags"  style="width:35%" data-options="
											                url: 'headQCustMgmtJSONAction!listCusTags',
											                method: 'get',
											                groupField:'groupName',
											                value: '',
											                valueField: 'id',
											                textField: 'tagName',
											                limitToList: true,
											                hasDownArrow: true,
											                prompt: '请选择客户标签',
											                tagStyler: function(value){
										                    if (value >0 && value <=3){
										                        return 'background:#ffd7d7;color:#c65353';
										                    } else if (value >= 4 && value <17){
										                        return 'background:#76EEC6;color:#c65353';
										                    }else if (value >= 17 && value <19){
										                        return 'background:#7EC0EE;color:#c65353';
										                    }else if (value >= 19 ){
										                        return 'background:#EEB422;color:#c65353';
										                    }
										                }
											                ">
														</td>		
												

											</tr>

										</table>
									</td>
								</tr>
							</table>
						</td>
					</tr>
				</table>

			</s:form>
		</div>
		<div data-options="region:'center',border:false">
			<table id="dataGrid" border="0">
			</table>
			<div id="toolbar" style="display: none;">
				<a onclick="generateReport();" href="javascript:void(0);"
					class="easyui-linkbutton"
					data-options="iconCls:'icon-search',plain:true">查询报表</a>
				 <a
					onclick="downloadReport();" href="javascript:void(0);"
					class="easyui-linkbutton"
					data-options="plain:true,iconCls:'icon-print'">下载报表</a>	
			</div>
		</div>
	</div>
</body>
</html>